#include "Panel.h"

#include <stdio.h>

#include "../_Luly.h"
#include "_UI.h"
Panel::Panel(std::string uiObjectId, const int x, const int y, const int width,
             const int height, double scale, double angle)
    : UIObject(uiObjectId, x, y, width, height, scale, angle) {
  layout = new Layout(uiObjectId + "_layout_", Layout::Arrangement::VERTICAL);
  UIObject::addChild(layout);
}

void Panel::init() {
  ninePatch = new NinePatch(_UI::getInstance().panel_60x60_ninePatch);
  //初始宽高
  width = ninePatch->getContentToLeft() + ninePatch->getContentToRight();
  height = ninePatch->getContentToBottom() + ninePatch->getContentToTop();
  contentX = ninePatch->getContentToLeft();
  contentY = ninePatch->getContentToTop();
  layout->setRelativeXY(contentX, contentY);
  UIObject::init();
}

void Panel::update() { UIObject::update(); }

void Panel::updateContentData() {
  // contentX = ninePatch->getContentToLeft();
  // contentY = ninePatch->getContentToTop();
  // if (layout->getWidthMode() == WHMode::FIT) {
  //   width = layout->getWidth() + ninePatch->getContentToLeft() +
  //           ninePatch->getContentToRight();
  //   contentWidth = layout->getWidth();
  // } else {
  //   contentWidth =
  //       width - ninePatch->getContentToLeft() - ninePatch->getContentToRight();
  // }

  // if (layout->getHeightMode() == WHMode::FIT) {
  //   height = layout->getHeight() + ninePatch->getContentToTop() +
  //            ninePatch->getContentToBottom();
  //   contentHeight = layout->getHeight();
  // } else {
  //   contentHeight =
  //       height - ninePatch->getContentToTop() - ninePatch->getContentToBottom();
  // }

  // layout->setRelativeXY(contentX, contentY);
  UIObject::updateContentData();
}

void Panel::draw() {
  // contentX = ninePatch->getContentToLeft();
  // contentY = ninePatch->getContentToTop();

  // if (layout->getWidthMode() == WHMode::FIT) {
  //   width = layout->getWidth() + ninePatch->getContentToLeft() +
  //           ninePatch->getContentToRight();
  //   contentWidth = layout->getWidth();
  // } else {
  //   contentWidth =
  //       width - ninePatch->getContentToLeft() -
  //       ninePatch->getContentToRight();
  // }

  // if (layout->getHeightMode() == WHMode::FIT) {
  //   height = layout->getHeight() + ninePatch->getContentToTop() +
  //            ninePatch->getContentToBottom();
  //   contentHeight = layout->getHeight();
  // } else {
  //   contentHeight =
  //       height - ninePatch->getContentToTop() -
  //       ninePatch->getContentToBottom();
  // }

  // layout->setRelativeXY(contentX, contentY);
  // ninePatch->drawContent(x, y, contentWidth, contentHeight);

  // UIObject::draw();
  Panel::drawArea(Rect{x, y, width, height});
}

void Panel::drawArea(const Rect& area) {
  contentX = ninePatch->getContentToLeft();
  contentY = ninePatch->getContentToTop();

  if (layout->getWidthMode() == WHMode::FIT) {
    width = layout->getWidth() + ninePatch->getContentToLeft() +
            ninePatch->getContentToRight();
    contentWidth = layout->getWidth();
  } else {
    contentWidth =
        width - ninePatch->getContentToLeft() - ninePatch->getContentToRight();
    if(layout->getWidth()>contentWidth){
      layout->setWidth(contentWidth);
    }
  }

  if (layout->getHeightMode() == WHMode::FIT) {
    height = layout->getHeight() + ninePatch->getContentToTop() +
             ninePatch->getContentToBottom();
    contentHeight = layout->getHeight();
  } else {
    contentHeight =
        height - ninePatch->getContentToTop() - ninePatch->getContentToBottom();
            if(layout->getWidth()>contentHeight){
      layout->setHeight(contentHeight);
    }
  }
  
  layout->setRelativeXY(contentX, contentY);
  ninePatch->drawContentArea(area, x, y, contentWidth, contentHeight);
  // printf("%s,%d\n",uiObjectId.c_str(),width);

  UIObject::drawArea(area);
}

Panel::~Panel() { delete ninePatch; }